package com.social.instadownloader.screen;

import android.app.Dialog;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

import com.social.instadownloader.R;
import com.social.instadownloader.screen.common.InstaFragmentScreen;
import com.social.instadownloader.util.ClipboardUtil;
import com.social.instadownloader.util.Launcher;
import com.social.instadownloader.widget.CirclePageIndicator;

public class HowItWorkScreen extends InstaFragmentScreen{

	/** The image. */
	private final int[] IMAGE = { R.drawable.dr_002_copy_share_url,
			R.drawable.dr_003_wait_copy, R.drawable.dr_004_taskmanager,
			R.drawable.dr_005_paste, R.drawable.dr_008_wait_checking,
			R.drawable.dr_009_available_resource, R.drawable.dr_011_history,};
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.how_it_work_screen, container,false);
		v.findViewById(R.id.btnCopyOrShare).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ClipboardUtil.putText(getActivity(), getResources().getString(R.string.guide_hint_copy1));
			}
		});
		v.findViewById(R.id.btnViewBasicSteps).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showBasicStepDialog();
			}
		});
		return v;
	}

	public void showBasicStepDialog(){
		final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Light); 
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
		dialog.setCanceledOnTouchOutside(false);
		
		View v = LayoutInflater.from(getActivity()).inflate(R.layout.basic_step_dialog, null);
		ViewPagerAdapter mAdapter = new ViewPagerAdapter(this, IMAGE);
		ViewPager mViewPager = (ViewPager) v.findViewById(R.id.pager);
		mViewPager.setAdapter(mAdapter);

		CirclePageIndicator mPageIndicator = (CirclePageIndicator) v.findViewById(R.id.indicator);
		mPageIndicator.setViewPager(mViewPager);
		ImageView imgClose = (ImageView)v.findViewById(R.id.imgClose);
		imgClose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.setContentView(v); 
		dialog.show();
	}
	
	@Override
	public void onLeftIconClick(View v) {
		Launcher.launchPackageInMarket(getActivity(),getActivity().getPackageName());
	}
	
	@Override
	public void onRightIconClick(View v) {
		Launcher.launchInstagram(getActivity());
	}
	@Override
	public String getScreenName() {
		return "How It Work";
	}
	
	public class ViewPagerAdapter extends PagerAdapter{

		private Fragment mFrag;
		private int[] images;
    
		public ViewPagerAdapter(Fragment frag,int[] drawables) {
			mFrag = frag;
			images = drawables;
		}
    
		 public int getCount() {
			return images.length;
		 }

			public Object instantiateItem(View collection, int position) {
				  ImageView view = new ImageView(mFrag.getActivity());
				  view.setImageResource(images[position]);
				  ((ViewPager) collection).addView(view, 0);
				  return view;
			 }

			 @Override
			 public void destroyItem(View arg0, int arg1, Object arg2) {
			  ((ViewPager) arg0).removeView((View) arg2);
			 }

			 @Override
			 public boolean isViewFromObject(View arg0, Object arg1) {
			  return arg0 == ((View) arg1);
			 }

			 @Override
			 public Parcelable saveState() {
			  return null;
			 }
		 
	}

}
